/*
 *	@file	safario_data.c
 *	@brief	サファリオブジェレイアウト　データ定義
 *	@author	Miyuki Iwasawa
 *	@date	08.10.15
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/brightness.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/window.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/wordset.h"
#include "system/buflen.h"
#include "gflib/touchpanel.h"
#include "gflib/msg_print.h"
#include "gflib/strbuf_family.h"
#include "application/app_tool.h"
#include "savedata/config.h"

#include "application/safari_l.h"

#include "safario_sys.h"
#include "safario_sub.h"
#include "safario_objlist.h"

typedef struct _SAFARI_OBJ_ADD_TBL{
	u8 num;
	const u8* list;
}SAFARI_OBJ_ADD_TBL;

typedef enum{
 SOAT_GRASS,
 SOAT_TREE,
 SOAT_ROCK,
 SOAT_WATER,
}SOAT_TBL;

#if 0
static const u8 safari_obj_add_list_default[] = {
 SAFARI_OBJ_TREE,
 SAFARI_OBJ_BENCH,
 SAFARI_OBJ_STATUE,
};

#define SAFARI_OBJ_ADDLIST_DEF_NUM (sizeof(safari_obj_add_list_default))

#define SAFARI_OBJ_ADDLIST_DEF ( \
 (1<<SAFARI_OBJ_TREE)|(1<<SAFARI_OBJ_BENCH)|(1<<SAFARI_OBJ_STATUE))

#endif
static const u8 safari_obj_add_list_grass[] = {
 SAFARI_OBJ_BUSH,
 SAFARI_OBJ_FLOWER_R,
 SAFARI_OBJ_FLOWER_W,
 SAFARI_OBJ_FLAG,
 SAFARI_OBJ_LIGHT,
 SAFARI_OBJ_BENCH,
};

static const u8 safari_obj_add_list_tree[] = {
 SAFARI_OBJ_TREE,
 SAFARI_OBJ_STUMP,
 SAFARI_OBJ_BRANCH,
 SAFARI_OBJ_FANCE_SS,
 SAFARI_OBJ_FANCE_LS,
 SAFARI_OBJ_SIGNB,
};

static const u8 safari_obj_add_list_rock[] = {
 SAFARI_OBJ_ROCK_S,
 SAFARI_OBJ_ROCK_L,
 SAFARI_OBJ_ROCK_MOSS,
 SAFARI_OBJ_GUIDE_R,
 SAFARI_OBJ_GUIDE_L,
 SAFARI_OBJ_TRASHBOX,
};

static const u8 safari_obj_add_list_water[] = {
 SAFARI_OBJ_PUDDLE,
 SAFARI_OBJ_FOUNTAIN,
 SAFARI_OBJ_WATER_HOLE,
 SAFARI_OBJ_FANCE_SL,
 SAFARI_OBJ_FANCE_LL,
 SAFARI_OBJ_STATUE,
};

static const SAFARI_OBJ_ADD_TBL safari_obj_add_tbl[] = {
 { sizeof(safari_obj_add_list_grass), safari_obj_add_list_grass},
 { sizeof(safari_obj_add_list_tree), safari_obj_add_list_tree},
 { sizeof(safari_obj_add_list_rock), safari_obj_add_list_rock},
 { sizeof(safari_obj_add_list_water), safari_obj_add_list_water},
};

static const u8 safari_obj_add_list_tbl[][4] = {
 {SOAT_GRASS,SOAT_TREE,SOAT_ROCK,SOAT_WATER},
 {SOAT_GRASS,SOAT_TREE,SOAT_ROCK,SOAT_WATER},
 {SOAT_GRASS,SOAT_TREE,SOAT_ROCK,SOAT_WATER},

 {SOAT_TREE,SOAT_ROCK,SOAT_WATER,SOAT_GRASS},
 {SOAT_TREE,SOAT_ROCK,SOAT_WATER,SOAT_GRASS},
 {SOAT_TREE,SOAT_ROCK,SOAT_WATER,SOAT_GRASS},

 {SOAT_ROCK,SOAT_WATER,SOAT_GRASS,SOAT_TREE},
 {SOAT_ROCK,SOAT_WATER,SOAT_GRASS,SOAT_TREE},

 {SOAT_WATER,SOAT_GRASS,SOAT_TREE,SOAT_ROCK},
 {SOAT_WATER,SOAT_GRASS,SOAT_TREE,SOAT_ROCK},
};

static u8 get_obj_siz_id(u8 sx,u8 sy);

/**
 *	@brief	表示リスト生成
 */
SAFARI_OBJ_LIST* SafariObjData_AllocObjList(u32 tr_id,u8 level,u8 my_sex,u8* p_num,u32 heapID)
{
	int i,j;
	SAFARI_OBJ_LIST* list;
	const SAFARI_OBJ_ADD_TBL* tbl;
	const u8* addl_tbl;

	u8 total = 0;	//SAFARI_OBJ_ADDLIST_DEF_NUM;
	u32 open_f = 0;	//SAFARI_OBJ_ADDLIST_DEF;

	addl_tbl = safari_obj_add_list_tbl[tr_id%10];

	for(i = 0;i < level;i++){
		tbl = &safari_obj_add_tbl[addl_tbl[i]];
		for(j = 0;j < tbl->num ;j++){
			open_f |= (1<<tbl->list[j]);
			total++;
		}
	}
	list = sys_AllocMemory(heapID,sizeof(SAFARI_OBJ_LIST)*total);
	MI_CpuClear8(list,sizeof(SAFARI_OBJ_LIST)*total);

	total  =0;
	for(i = 0;i < SAFARI_OBJ_ID_MAX;i++){
		if(open_f & 1){
			list[total].id = i;
			SafariData_GetObjectParam(&list[total].prm,i,my_sex);
			list[total].siz = get_obj_siz_id(list[total].prm.siz_x,list[total].prm.siz_z);
			total++;
		}
		open_f >>= 1;
	}
	*p_num = total;
	return list;
}

static u8 get_obj_siz_id(u8 sx,u8 sy)
{
	if(sx == 1){
		if(sy == 1){
			return SAFOBJ_SIZ_1x1;
		}
		return SAFOBJ_SIZ_1x2;
	}else if(sy == 1){
		return SAFOBJ_SIZ_2x1;
	}
	return SAFOBJ_SIZ_2x2;
}